
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>ButterKnife · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="thread.html" />
    
    
    <link rel="prev" href="json-reporter.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">----安卓 UI----</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="view.html">
            
                <a href="view.html">
            
                    
                    自定义控件和measure
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="canvas-paint.html">
            
                <a href="canvas-paint.html">
            
                    
                    draw和canvas，paint详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="anim.html">
            
                <a href="anim.html">
            
                    
                    动画
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="notify.html">
            
                <a href="notify.html">
            
                    
                    notify系列库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="loading.html">
            
                <a href="loading.html">
            
                    
                    loading系列库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="status-ui.html">
            
                <a href="status-ui.html">
            
                    
                    StatusUI系列库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="touch-scroll.html">
            
                <a href="touch-scroll.html">
            
                    
                    滑动
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="layout.html">
            
                <a href="layout.html">
            
                    
                    Layout
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="textview.html">
            
                <a href="textview.html">
            
                    
                    TextView
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="drawable.html">
            
                <a href="drawable.html">
            
                    
                    Drawable
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="item-template.html">
            
                <a href="item-template.html">
            
                    
                    列表Adapter模板化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="recyclerview.html">
            
                <a href="recyclerview.html">
            
                    
                    RecyclerView全解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="design.html">
            
                <a href="design.html">
            
                    
                    MeterialDesign详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="component.html">
            
                <a href="component.html">
            
                    
                    AyoComponent基础库--Activity和Fragment
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="systembartint.html">
            
                <a href="systembartint.html">
            
                    
                    SystembarTint
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="permission-ask.html">
            
                <a href="permission-ask.html">
            
                    
                    权限控制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="keyboard.html">
            
                <a href="keyboard.html">
            
                    
                    小键盘
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="notification.html">
            
                <a href="notification.html">
            
                    
                    Notification
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">----安卓 foundation----</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="mvp.html">
            
                <a href="mvp.html">
            
                    
                    MVP
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="rxjava.html">
            
                <a href="rxjava.html">
            
                    
                    RxJava
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="lang.html">
            
                <a href="lang.html">
            
                    
                    常用工具类
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="http.html">
            
                <a href="http.html">
            
                    
                    http
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="xml.html">
            
                <a href="xml.html">
            
                    
                    xml解析
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="json-reporter.html">
            
                <a href="json-reporter.html">
            
                    
                    Json解析
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="2.7" data-path="inject.html">
            
                <a href="inject.html">
            
                    
                    ButterKnife
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="thread.html">
            
                <a href="thread.html">
            
                    
                    安卓线程和looper
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="process.html">
            
                <a href="process.html">
            
                    
                    多进程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="eventbus.html">
            
                <a href="eventbus.html">
            
                    
                    EventBus
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="db.html">
            
                <a href="db.html">
            
                    
                    数据库和ContentProvider
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="job.html">
            
                <a href="job.html">
            
                    
                    定时任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13" data-path="service.html">
            
                <a href="service.html">
            
                    
                    service
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.14" data-path="opt-mem.html">
            
                <a href="opt-mem.html">
            
                    
                    内存优化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.15" data-path="opt-network.html">
            
                <a href="opt-network.html">
            
                    
                    网络优化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.16" data-path="opt-anr.html">
            
                <a href="opt-anr.html">
            
                    
                    anr排查
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.17" data-path="opt-ui.html">
            
                <a href="opt-ui.html">
            
                    
                    UI卡顿优化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.18" data-path="opt-launch.html">
            
                <a href="opt-launch.html">
            
                    
                    启动优化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.19" data-path="opt-launch.html">
            
                <a href="opt-launch.html">
            
                    
                    混淆
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.20" data-path="hotfix.html">
            
                <a href="hotfix.html">
            
                    
                    热补丁混淆
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.21" data-path="aar.html">
            
                <a href="aar.html">
            
                    
                    aar相关
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.22" data-path="route.html">
            
                <a href="route.html">
            
                    
                    Route路由
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.23" data-path="modules.html">
            
                <a href="modules.html">
            
                    
                    分模块，组件化，插件化
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">----Gradle----</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="gradle.html">
            
                <a href="gradle.html">
            
                    
                    gradle基本配置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="gradle-plugin.html">
            
                <a href="gradle-plugin.html">
            
                    
                    gradle插件开发
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">----工具----</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="repos.html">
            
                <a href="repos.html">
            
                    
                    优秀博客，开源项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="packs.html">
            
                <a href="packs.html">
            
                    
                    自动打包，多渠道打包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="gist.html">
            
                <a href="gist.html">
            
                    
                    代码片段和常见问题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="stetho.html">
            
                <a href="stetho.html">
            
                    
                    stetho
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="fabric.html">
            
                <a href="fabric.html">
            
                    
                    fabric
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="app-update.html">
            
                <a href="app-update.html">
            
                    
                    自动升级
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.7" data-path="bugsnags.html">
            
                <a href="bugsnags.html">
            
                    
                    日志和崩溃统计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.8" data-path="mail.html">
            
                <a href="mail.html">
            
                    
                    发邮件辅助接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.9" data-path="socail.html">
            
                <a href="socail.html">
            
                    
                    第三方登录和分享
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.10" data-path="pay-alipay.html">
            
                <a href="pay-alipay.html">
            
                    
                    支付宝支付
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.11" data-path="pay-wx.html">
            
                <a href="pay-wx.html">
            
                    
                    微信支付
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.12" data-path="umeng.html">
            
                <a href="umeng.html">
            
                    
                    友盟统计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.13" data-path="push-jpush.html">
            
                <a href="push-jpush.html">
            
                    
                    推送-极光
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.14" data-path="push-xiaomi.html">
            
                <a href="push-xiaomi.html">
            
                    
                    推送-小米
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >ButterKnife</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="butterknife">ButterKnife</h1>
<h1 id="&#x63A7;&#x4EF6;&#x6CE8;&#x5165;">&#x63A7;&#x4EF6;&#x6CE8;&#x5165;</h1>
<ul>
<li>&#x63A7;&#x4EF6;&#x6CE8;&#x5165;<ul>
<li>XUtils&#x7684;&#x6CE8;&#x5165;&#x529F;&#x80FD;</li>
<li>ButterKinfe</li>
<li>&#x57FA;&#x4E8E;&#x6027;&#x80FD;&#x7684;&#x8003;&#x91CF;</li>
</ul>
</li>
</ul>
<p>XUtils&#x7684;&#x4E0D;&#x518D;&#x505A;&#x4ECB;&#x7ECD;&#xFF0C;&#x5EFA;&#x8BAE;&#x4F7F;&#x7528;ButterKnife&#xFF0C;&#x867D;&#x7136;&#x6CA1;&#x6709;&#x76F4;&#x63A5;&#x7684;&#x8BC1;&#x636E;&#x8868;&#x660E;&#x4E8C;&#x8005;&#x6027;&#x80FD;&#x4E0A;&#x6709;&#x660E;&#x663E;&#x5DEE;&#x5F02;&#xFF0C;&#x4F46;
ButterKnife&#x660E;&#x663E;&#x6BD4;XUtils&#x6D41;&#x884C;&#xFF0C;&#x800C;&#x4E14;&#x6709;&#x66F4;&#x591A;&#x7684;&#x63D2;&#x4EF6;&#x652F;&#x6301;&#xFF0C;&#x529F;&#x80FD;&#x4E5F;&#x66F4;&#x591A;</p>
<ul>
<li>ButterKnife&#xFF1A;<ul>
<li>&#x6587;&#x6863;&#xFF1A;<a href="http://jakewharton.github.io/butterknife/" target="_blank">http://jakewharton.github.io/butterknife/</a></li>
<li>Github&#xFF1A;<a href="https://github.com/JakeWharton/butterknife" target="_blank">https://github.com/JakeWharton/butterknife</a></li>
<li>&#x63D2;&#x4EF6;&#xFF1A;<a href="http://blog.csdn.net/jdsjlzx/article/details/49101433" target="_blank">http://blog.csdn.net/jdsjlzx/article/details/49101433</a></li>
</ul>
</li>
</ul>
<ul>
<li>XUtils&#x548C;ButterKnife&#x5BF9;&#x6BD4;&#xFF1A;<ul>
<li>&#x4E8C;&#x8005;&#x90FD;&#x662F;&#x53CD;&#x5C04;&#x51FA;&#x6CE8;&#x89E3;&#xFF0C;&#x7136;&#x540E;findViewById</li>
<li>XUtils&#x8FD8;&#x6D89;&#x53CA;&#x5230;&#x4E86;parent&#xFF1F;&#x5565;&#x610F;&#x601D;&#xFF1F;</li>
<li>ButterKnife&#x6709;&#x7F13;&#x5B58;&#xFF0C;XUtils&#x4E0D;&#x77E5;&#x9053;&#x6709;&#x6CA1;&#x6709;</li>
<li>&#x603B;&#x662F;&#x7528;XUtils&#x7684;&#x4E1C;&#x897F;&#x4E5F;&#x6CA1;&#x610F;&#x601D;&#xFF0C;&#x80FD;&#x4E0D;&#x7528;&#x5C31;&#x4E0D;&#x7528;&#x5427;</li>
</ul>
</li>
</ul>
<h1 id="butterknife&#x7B80;&#x964B;&#x53C2;&#x8003;&#x624B;&#x518C;&#xFF1A;&#x6587;&#x6863;&#x7684;&#x7B80;&#x5355;&#x7FFB;&#x8BD1;">ButterKnife&#x7B80;&#x964B;&#x53C2;&#x8003;&#x624B;&#x518C;&#xFF1A;&#x6587;&#x6863;&#x7684;&#x7B80;&#x5355;&#x7FFB;&#x8BD1;</h1>
<h2 id="1-&#x63A7;&#x4EF6;&#x7ED1;&#x5B9A;">1 &#x63A7;&#x4EF6;&#x7ED1;&#x5B9A;</h2>
<pre><code class="lang-java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ExampleActivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Activity</span> </span>{
  <span class="hljs-meta">@Bind</span>(R.id.title) TextView title;
  <span class="hljs-meta">@Bind</span>(R.id.subtitle) TextView subtitle;
  <span class="hljs-meta">@Bind</span>(R.id.footer) TextView footer;

  <span class="hljs-meta">@Override</span> <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onCreate</span><span class="hljs-params">(Bundle savedInstanceState)</span> </span>{
    <span class="hljs-keyword">super</span>.onCreate(savedInstanceState);
    setContentView(R.layout.simple_activity);
    ButterKnife.bind(<span class="hljs-keyword">this</span>);
    <span class="hljs-comment">// TODO Use fields...</span>
  }
}

&#x751F;&#x6210;&#x7684;&#x4EE3;&#x7801;&#x7C7B;&#x4F3C;&#x4E0B;&#x9762;:
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">bind</span><span class="hljs-params">(ExampleActivity activity)</span> </span>{
  activity.subtitle = (android.widget.TextView) activity.findViewById(<span class="hljs-number">2130968578</span>);
  activity.footer = (android.widget.TextView) activity.findViewById(<span class="hljs-number">2130968579</span>);
  activity.title = (android.widget.TextView) activity.findViewById(<span class="hljs-number">2130968577</span>);
}
</code></pre>
<h2 id="2-&#x8D44;&#x6E90;&#x7684;&#x6CE8;&#x5165;">2 &#x8D44;&#x6E90;&#x7684;&#x6CE8;&#x5165;</h2>
<pre><code class="lang-java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ExampleActivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Activity</span> </span>{
  <span class="hljs-meta">@BindString</span>(R.string.title) String title;
  <span class="hljs-meta">@BindDrawable</span>(R.drawable.graphic) Drawable graphic;
  <span class="hljs-meta">@BindColor</span>(R.color.red) <span class="hljs-keyword">int</span> red; <span class="hljs-comment">// int or ColorStateList field</span>
  <span class="hljs-meta">@BindDimen</span>(R.dimen.spacer) Float spacer; <span class="hljs-comment">// int (for pixel size) or float (for exact value) field</span>
  <span class="hljs-comment">// ...</span>
}
</code></pre>
<h2 id="3-&#x65E0;activity&#xFF1A;view&#x7684;&#x7ED1;&#x5B9A;">3 &#x65E0;Activity&#xFF1A;View&#x7684;&#x7ED1;&#x5B9A;</h2>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FancyFragment</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Fragment</span> </span>{
  <span class="hljs-meta">@Bind</span>(R.id.button1) Button button1;
  <span class="hljs-meta">@Bind</span>(R.id.button2) Button button2;

  <span class="hljs-meta">@Override</span> <span class="hljs-function"><span class="hljs-keyword">public</span> View <span class="hljs-title">onCreateView</span><span class="hljs-params">(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)</span> </span>{
    View view = inflater.inflate(R.layout.fancy_fragment, container, <span class="hljs-keyword">false</span>);
    ButterKnife.bind(<span class="hljs-keyword">this</span>, view);
    <span class="hljs-comment">// TODO Use fields...</span>
    <span class="hljs-keyword">return</span> view;
  }

  <span class="hljs-meta">@Override</span> <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onDestroyView</span><span class="hljs-params">()</span> </span>{
      <span class="hljs-keyword">super</span>.onDestroyView();
      ButterKnife.unbind(<span class="hljs-keyword">this</span>);
  }
}
</code></pre>
<h2 id="4-viewholder&#x7684;&#x7ED1;&#x5B9A;">4 ViewHolder&#x7684;&#x7ED1;&#x5B9A;</h2>
<pre><code class="lang-java"><span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ViewHolder</span> </span>{
    <span class="hljs-meta">@Bind</span>(R.id.title) TextView name;
    <span class="hljs-meta">@Bind</span>(R.id.job_title) TextView jobTitle;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">ViewHolder</span><span class="hljs-params">(View view)</span> </span>{
      ButterKnife.bind(<span class="hljs-keyword">this</span>, view);
    }
}
</code></pre>
<h3 id="5-&#x4E8B;&#x4EF6;&#x7ED1;&#x5B9A;">5 &#x4E8B;&#x4EF6;&#x7ED1;&#x5B9A;</h3>
<pre><code class="lang-java"><span class="hljs-meta">@OnClick</span>(R.id.submit)
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">submit</span><span class="hljs-params">(View view)</span> </span>{
  <span class="hljs-comment">// TODO submit data to server...</span>
}

<span class="hljs-meta">@OnClick</span>(R.id.submit)
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">submit</span><span class="hljs-params">()</span> </span>{
  <span class="hljs-comment">// TODO submit data to server...</span>
}

<span class="hljs-meta">@OnClick</span>(R.id.submit)
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">sayHi</span><span class="hljs-params">(Button button)</span> </span>{
  button.setText(<span class="hljs-string">&quot;Hello!&quot;</span>);
}

<span class="hljs-meta">@OnClick</span>({ R.id.door1, R.id.door2, R.id.door3 })
<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">pickDoor</span><span class="hljs-params">(DoorView door)</span> </span>{
  <span class="hljs-keyword">if</span> (door.hasPrizeBehind()) {
    Toast.makeText(<span class="hljs-keyword">this</span>, <span class="hljs-string">&quot;You win!&quot;</span>, LENGTH_SHORT).show();
  } <span class="hljs-keyword">else</span> {
    Toast.makeText(<span class="hljs-keyword">this</span>, <span class="hljs-string">&quot;Try again&quot;</span>, LENGTH_SHORT).show();
  }
}

<span class="hljs-comment">//&#x6CE8;&#x610F;&#xFF0C;&#x81EA;&#x5B9A;&#x4E49;&#x63A7;&#x4EF6;&#xFF0C;&#x7ED9;&#x81EA;&#x5DF1;&#x7ED1;&#x5B9A;&#x4E8B;&#x4EF6;&#xFF0C;&#x4E0D;&#x7528;&#x6307;&#x5B9A;id</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FancyButton</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Button</span> </span>{
  <span class="hljs-meta">@OnClick</span>
  <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onClick</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-comment">// TODO do something!</span>
  }
}
</code></pre>
<p><strong>&#x8FD9;&#x4E2A;&#x4E0D;&#x77E5;&#x9053;&#x5565;&#x610F;&#x601D;</strong></p>
<pre><code>MULTI-METHOD LISTENERS

Method annotations whose corresponding listener has multiple callbacks can be used to bind to any one of them. Each annotation has a default callback that it binds to. Specify an alternate using the callback parameter.

@OnItemSelected(R.id.list_view)
void onItemSelected(int position) {
  // TODO ...
}

@OnItemSelected(value = R.id.maybe_missing, callback = NOTHING_SELECTED)
void onNothingSelected() {
  // TODO ...
}
</code></pre><h3 id="6-&#x7279;&#x6B8A;&#x8BED;&#x6CD5;">6 &#x7279;&#x6B8A;&#x8BED;&#x6CD5;</h3>
<p><strong>List<view>&#x7684;&#x7ED1;&#x5B9A;</view></strong></p>
<pre><code class="lang-java"><span class="hljs-meta">@Bind</span>({ R.id.first_name, R.id.middle_name, R.id.last_name })
List&lt;EditText&gt; nameViews;
</code></pre>
<p><strong>apply&#x65B9;&#x6CD5;</strong></p>
<pre><code class="lang-java">ButterKnife.apply(nameViews, DISABLE);
ButterKnife.apply(nameViews, ENABLED, <span class="hljs-keyword">false</span>);

<span class="hljs-comment">//Action and Setter interfaces allow specifying simple behavior.</span>

<span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> ButterKnife.Action&lt;View&gt; DISABLE = <span class="hljs-keyword">new</span> ButterKnife.Action&lt;View&gt;() {
  <span class="hljs-meta">@Override</span> <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">apply</span><span class="hljs-params">(View view, <span class="hljs-keyword">int</span> index)</span> </span>{
    view.setEnabled(<span class="hljs-keyword">false</span>);
  }
};
<span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> ButterKnife.Setter&lt;View, Boolean&gt; ENABLED = <span class="hljs-keyword">new</span> ButterKnife.Setter&lt;View, Boolean&gt;() {
  <span class="hljs-meta">@Override</span> <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">set</span><span class="hljs-params">(View view, Boolean value, <span class="hljs-keyword">int</span> index)</span> </span>{
    view.setEnabled(value);
  }
};

<span class="hljs-comment">//An Android Property can also be used with the apply method.</span>

ButterKnife.apply(nameViews, View.ALPHA, <span class="hljs-number">0.0f</span>);
</code></pre>
<p><strong>&#x8FD9;&#x4E2A;&#x4E5F;&#x4E0D;&#x77E5;&#x9053;&#x5565;&#x610F;&#x601D;</strong></p>
<pre><code class="lang-java">BONUS

Also included are findById methods which simplify code that still has to find views on a View, Activity, or Dialog. It uses generics to infer the <span class="hljs-keyword">return</span> type and automatically performs the cast.

View view = LayoutInflater.from(context).inflate(R.layout.thing, <span class="hljs-keyword">null</span>);
TextView firstName = ButterKnife.findById(view, R.id.first_name);
TextView lastName = ButterKnife.findById(view, R.id.last_name);
ImageView photo = ButterKnife.findById(view, R.id.photo);
<span class="hljs-comment">//Add a static import for ButterKnife.findById and enjoy even more fun.</span>
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="json-reporter.html" class="navigation navigation-prev " aria-label="Previous page: Json解析">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="thread.html" class="navigation navigation-next " aria-label="Next page: 安卓线程和looper">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"ButterKnife","level":"2.7","depth":1,"next":{"title":"安卓线程和looper","level":"2.8","depth":1,"path":"thread.md","ref":"thread.md","articles":[]},"previous":{"title":"Json解析","level":"2.6","depth":1,"path":"json-reporter.md","ref":"json-reporter.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"inject.md","mtime":"2017-04-09T14:46:41.969Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-04-09T17:03:25.521Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

